/*============================================================================*/
/*                                                                            */
/* evaldet.c                                                                  */
/*                                                                            */
/* This program evaluates the results of corner detection. It requires        */
/* two files as an input. The first file contains the list of corners that    */
/* is to be evaluated. The second file contains a reference list of corners   */
/* that is considered to be a correct solution. The files are compared,       */
/* the differences are evaluated and printed out. The details about how       */
/* the comparison is carried out can be found in technical report referred    */
/* in "cordet.c".                                                             */
/*                                                                            */
/* (c) Eduard Sojka, 2002                                                     */
/*                                                                            */
/*============================================================================*/

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "utils.h"
#define  MAX_NUM_OF_CORNER_COORDS 20000 /* max num of corners coords allowed  */
#define  MAX_DIST 4                     /* max allowed distance between true  */
                                        /* and detected position of a corner  */
/*============================================================================*/

#define freeMem \
if (actSol != NULL) free(actSol);\
if (refSol != NULL) free(refSol);

int main( int argc, char **argv ) {

    int  *actSol, actSolLen, *refSol, refSolLen;
    int   foundCorrs, missedCorrs, falseDets, multipleDets, totalErr;
    float locErr;

    if ( argc != 3 ){
      printf( "\nsyntax:\n\n" );
      printf( "  evaldet file1 file2\n\n" );
      printf( "  file1: Evaluated corner list\n" );
      printf( "  file2: Reference corner list\n\n");
      printf( "example: evaldet result1.txt refsol1.txt\n\n" );
      return( -1 );
    }

    printf("\n");
    printf("===================================\n");
    printf("| Evaluating the corner detection |\n");
    printf("===================================\n");

    actSol = malloc(MAX_NUM_OF_CORNER_COORDS* sizeof(actSol[0]));
    refSol = malloc(MAX_NUM_OF_CORNER_COORDS* sizeof(refSol[0]));
    if ((actSol == NULL)||(refSol == NULL)){
                          printf("Not enough memory to run.\n");
                          freeMem; return( -1 ); }

    actSolLen = loadCornerList(argv[1], actSol, MAX_NUM_OF_CORNER_COORDS);
    if (actSolLen == -1){ printf("Cannot open '%s'.\n", argv[1]);
                          freeMem; return(-1); }
    if (actSolLen == -2){ printf("Too many corners in '%s'.\n", argv[1]);
                          freeMem; return(-1); }

    refSolLen = loadCornerList(argv[2], refSol, MAX_NUM_OF_CORNER_COORDS);
    if (refSolLen == -1){ printf("Cannot open '%s'.\n", argv[2]);
                          freeMem; return(-1); }
    if (refSolLen == -2){ printf("Too many corners in '%s'.\n", argv[2]);
                          freeMem; return(-1); }

    evaluateDetection( actSol, actSolLen, refSol, refSolLen, &foundCorrs,
      &missedCorrs, &falseDets, &multipleDets, &totalErr, &locErr );

    printf("  Correct detections:        %i\n",   foundCorrs);
    printf("  Missed corners:            %i\n",   missedCorrs);
    printf("  False detections:          %i\n",   falseDets);
    printf("  Multiple detections:       %i\n",   multipleDets);
    printf("  Total error:               %i\n",   totalErr);
    printf("  Localisation error:        %.3f\n", locErr);
    printf("===================================\n\n");

    return 0;
}

/*============================================================================*/

